﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Web.Routing;
using System.Xml;
using Command;

namespace WebApp
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            ////判断dll目录在服务器路径，如果一致就不下需要修改webconfig
            //string s = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bin\\");
            //string conString1 = ConfigurationManager.AppSettings["AUTO_COMPLIE_DIRECTORY_PATH"];
            //if (!s.Equals(conString1))
            //{
            //    XmlDocument xmlDoc = new XmlDocument();
            //    string filename = Server.MapPath("/") + "Web.config";
            //    xmlDoc.Load(filename);
            //    XmlNode root = xmlDoc.SelectSingleNode("configuration");
            //    XmlNode conNode = root.SelectSingleNode("appSettings");
            //    XmlNodeList conNodeList = conNode.ChildNodes;
              
            //    foreach (XmlNode node in conNodeList)
            //    {
            //        XmlElement element = (XmlElement)node;  //将子节点类型转换为XmlElecment类型
            //        if (element.GetAttribute("key") == "AUTO_COMPLIE_DIRECTORY_PATH")
            //        {
            //            element.SetAttribute("value", s);
                    
            //        }
            //    }
               
            //    xmlDoc.Save(filename);
            //}
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            GlobalFilters.Filters.Add(new LogExceptionFilterAttribute() { Message = "Gloable" });
            RegisterGlobalFilters(GlobalFilters.Filters);
            LazyRabbit.Infrastructure.Dependency.DependencyContext.RegisterDependency();

        }
    }
}